﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using recordModel = OnlineManagement.Model.HybridizationRecord;
using OnlineManagement.DBUtility;
using System.Collections.Generic;//Please add references
using System.Linq;

namespace OnlineManagement.SQLServerDAL.Dal
{
	/// <summary>
    ///配种记录 数据访问类:HybridizationRecord
	/// </summary>
	public partial class HybridizationRecord:IDAL.IHybridizationRecord
	{
		public HybridizationRecord()
		{}

		#region  ExtensionMethod
        /// <summary>
        /// 获取空怀可配种母猪信息
        /// </summary>
        /// <param name="hoggeryId"></param>
        /// <returns></returns>
        public DataSet GetHybridizationSow(int hoggeryId) {
            string procName = Common.PubProcedures.GetHybridizationSowProc;
            SqlParameter[] paras = { new SqlParameter("@hoggeryId", SqlDbType.Int, 4) };
            paras[0].Value = hoggeryId;
            System.Data.DataSet ds = DbHelperSQL.RunProcedure(procName, paras);
            return ds;
        }

        /// <summary>
        /// 获取指定母猪信息，比如饲养员，猪圈名称，母猪品种
        /// </summary>
        /// <param name="hoggeryId"></param>
        /// <param name="sowId"></param>
        /// <returns></returns>
        public Model.SowInfoForHybrizationRecord GetHybridizationSowBasicInfo(int hoggeryId, Guid sowId)
        {
            string procName = Common.PubProcedures.GetHybridizationSowBasicInfo_Proc;
            SqlDataReader reader = null;
            Model.SowInfoForHybrizationRecord record = new Model.SowInfoForHybrizationRecord();
            IDataParameter[] parameters = new IDataParameter[] 
            { 
                new SqlParameter("@hoggeryId", SqlDbType.Int, 4),
                new SqlParameter("@sowId",SqlDbType.UniqueIdentifier,16)
            };
            parameters[0].Value = hoggeryId;
            parameters[1].Value = sowId;
            reader= DbHelperSQL.RunProcedure(procName, parameters);
            //DataSet ds = DbHelperSQL.RunProcedure(procName, parameters);
            if(reader!=null){
                while (reader.Read()) {
                    record.CustomizeID = string.IsNullOrEmpty(reader[0].ToString()) ? Common.PubConstant.EmptyString : reader[0].ToString();
                    record.FeederName = string.IsNullOrEmpty(reader[1].ToString()) ? Common.PubConstant.EmptyString : reader[1].ToString();
                    record.TypeName = string.IsNullOrEmpty(reader[2].ToString()) ? Common.PubConstant.EmptyString : reader[2].ToString(); 
                }
            }
            return record;
        }

        /// <summary>
        /// do not used any more
        /// </summary>
        /// <param name="record"></param>
        /// <param name="returnValue"></param>
        /// <returns></returns>
        public bool SowHybrizationAddOrUpdate(Model.HybridizationRecord record, out string returnValue)
        {
            try
            {
                returnValue = string.Empty;
                string procName = Common.PubProcedures.SowHybridizationRecordAddProce;
                SqlParameter[] parameters = { 
                    new SqlParameter("@hoggeryID", SqlDbType.Int,4),
					new SqlParameter("@sowID", SqlDbType.UniqueIdentifier,16),
                    new SqlParameter("@hybridizationDate", SqlDbType.Date,3),
					new SqlParameter("@boarBitID", SqlDbType.NVarChar,20),
					new SqlParameter("@boarPigType", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@Comment", SqlDbType.NVarChar,200),
                    new SqlParameter("@isHybridizateDone",SqlDbType.Bit,1),
                    new SqlParameter("@rowsAffected",SqlDbType.TinyInt,1),
                    new SqlParameter("@msg",SqlDbType.NVarChar,200)
                                        };
                parameters[0].Value = record.HoggeryID;
                parameters[1].Value = record.SowID;
                parameters[2].Value = record.HybridizationDate;
                parameters[3].Value = record.BoarBitID;
                parameters[4].Value = record.BoarPigType;
                parameters[5].Value = record.Comment;
                parameters[6].Value = record.IsHybridizateDone;
                parameters[7].Direction = ParameterDirection.Output;
                parameters[8].Direction = ParameterDirection.Output;
                int rowsAffected = 0;
                DbHelperSQL.RunProcedure(procName, parameters, out rowsAffected, out returnValue);
                return rowsAffected > 0 ? true : false;
            }catch(Exception ex){
                throw new Exception(Common.PubMsg.ServerError);
            }
        }

        public bool SowHybrizationAddOrUpdate(List<recordModel> records, 
                out string returnValue)
        {
            returnValue = string.Empty;
            int rowsAffected = 0;
            try
            {
                if (records != null && records.Count > 0)
                {
                    string procName = Common.PubProcedures.SowMultipileHybridizationRecordAddProce;
                    SqlParameter[] parameters = { 
                    new SqlParameter("@hoggeryID", SqlDbType.Int,4),
					new SqlParameter("@sowID", SqlDbType.UniqueIdentifier,16),
                    new SqlParameter("@hybridizationDate", SqlDbType.DateTime,8),
                    new SqlParameter("@hybridizationPerson",SqlDbType.NVarChar,20),
					new SqlParameter("@boarBitID", SqlDbType.NVarChar,20),
					new SqlParameter("@boarPigType", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@Comment", SqlDbType.NVarChar,200),

                    new SqlParameter("@secondHybridizationDate", SqlDbType.DateTime,8),
                    new SqlParameter("@secondHybridizationPerson",SqlDbType.NVarChar,20),
					new SqlParameter("@secondBoarBitID", SqlDbType.NVarChar,20),
					new SqlParameter("@secondBoarPigType", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@secondComment", SqlDbType.NVarChar,200),

                    new SqlParameter("@thirdHybridizationDate", SqlDbType.DateTime,8),
                    new SqlParameter("@thirdHybridizationPerson",SqlDbType.NVarChar,20),
					new SqlParameter("@thirdBoarBitID", SqlDbType.NVarChar,20),
					new SqlParameter("@thirdBoarPigType", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@thirdComment", SqlDbType.NVarChar,200),

                    new SqlParameter("@expectedDate",SqlDbType.Date,3),
                    new SqlParameter("@rowsAffected",SqlDbType.TinyInt,1),
                    new SqlParameter("@msg",SqlDbType.NVarChar,200)
                                        };

                    GenerateParas(records, ref parameters);
                    DbHelperSQL.RunProcedure(procName, parameters, out rowsAffected, out returnValue);
                }
                return rowsAffected > 0 ? true : false;
            }catch(Exception ex){
                throw new Exception(Common.PubMsg.ServerError);
            }
        }

        /// <summary>
        /// 获取指定母猪的配种信息,返回不定数目的表，每一个表包含配种全部信息
        /// </summary>
        /// <param name="hoggeryId"></param>
        /// <param name="sowId"></param>
        /// <param name="isHybridizateDone"></param>
        /// <returns></returns>
        public DataSet GetSowHybrizationRecords(int hoggeryId, Guid sowId, bool isHybridizateDone) {
            string proceName = Common.PubProcedures.GetSowSpecificHybrizationProce;
            SqlParameter[] parameters = { 
                    new SqlParameter("@hoggeryId", SqlDbType.Int,4),
					new SqlParameter("@sowId", SqlDbType.UniqueIdentifier,16),
                    new SqlParameter("@isCycleDone",SqlDbType.Bit,1),
                                        };
            parameters[0].Value = hoggeryId;
            parameters[1].Value = sowId;
            parameters[2].Value = isHybridizateDone;
            try
            {
                DataSet ds = DbHelperSQL.RunProcedure(proceName, parameters);
                return ds;
            }
            catch (Exception ex) 
            { 
                throw ex; 
            }

        }

        /// <summary>
        /// 母猪确认怀孕，需要先进行反情检查或者妊娠检查，当检查成功调用此方法
        /// </summary>
        /// <param name="hoggeryId"></param>
        /// <param name="sowId"></param>
        /// <param name="updateDate"></param>
        /// <returns></returns>
        public bool SowPregnancyRecord(int hoggeryId, Guid sowId, int isFanQing, DateTime updateDate, out string returnValue)
        {
            returnValue = string.Empty;
            string procName = Common.PubProcedures.SowFanQingValidateProce;
            SqlParameter[] parameters = { 
                    new SqlParameter("@hoggeryId", SqlDbType.Int,4),
					new SqlParameter("@sowId", SqlDbType.UniqueIdentifier,16),
                    new SqlParameter("@isFanQing",SqlDbType.Bit,1),
                    new SqlParameter("@updateDate", SqlDbType.Date,3),
                    new SqlParameter("@rowsAffected",SqlDbType.TinyInt,1),
                    new SqlParameter("@msg",SqlDbType.NVarChar,200)
                                        };
            parameters[0].Value = hoggeryId;
            parameters[1].Value = sowId;
            parameters[2].Value = isFanQing;
            parameters[3].Value = updateDate;
            parameters[4].Direction = ParameterDirection.Output;
            parameters[5].Direction = ParameterDirection.Output;

            try
            {
                int rowsAffected = 0;
                DbHelperSQL.RunProcedure(procName, parameters, out rowsAffected, out returnValue);
                return rowsAffected > 0 ? true : false;
            }catch(Exception ex){
                throw ex; 
            }
        }

        /// <summary>
        /// 母猪流产
        /// </summary>
        /// <param name="sowId"></param>
        /// <param name="updateDate"></param>
        /// <param name="returnValue"></param>
        /// <returns></returns>
        public bool SowAbortion(int hoggeryId, Guid sowId, out string returnValue)
        {
            returnValue = "";
            string proceName = Common.PubProcedures.SowAbortionProce;
            SqlParameter[] parameters = { 
                    new SqlParameter("@hoggeryId", SqlDbType.Int,4),
					new SqlParameter("@sowId", SqlDbType.UniqueIdentifier,16),
                    new SqlParameter("@rowsAffected",SqlDbType.TinyInt,1),
                    new SqlParameter("@msg",SqlDbType.NVarChar,200)
                                        };
            parameters[0].Value = hoggeryId;
            parameters[1].Value = sowId;
            parameters[2].Direction = ParameterDirection.Output;
            parameters[3].Direction = ParameterDirection.Output;
            try
            {
                int rowsAffected = 0;
                DbHelperSQL.RunProcedure(proceName, parameters, out rowsAffected, out returnValue);
                return rowsAffected > 0 ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        
		#endregion  ExtensionMethod

       
        #region private methods

        /// <summary>
        /// generate parameters for SowHybrizationAddOrUpdate
        /// </summary>
        /// <param name="records"></param>
        /// <param name="parameters"></param>
        private void GenerateParas(List<recordModel> records, ref SqlParameter[] parameters)
        {
            for (int i = 0; i < parameters.Count() - 2; i++)
            {
                parameters[i].Value = null;
            }
            parameters[18].Direction = ParameterDirection.Output;
            parameters[19].Direction = ParameterDirection.Output;
            foreach (var item in records)
            {
                switch (item.HybridizationCount)
                {
                    case 1:
                        parameters[0].Value = item.HoggeryID;
                        parameters[1].Value = item.SowID;
                        parameters[2].Value = item.HybridizationDate;
                        parameters[3].Value = item.HybridizationPerson;
                        parameters[4].Value = item.BoarBitID;
                        parameters[5].Value = item.BoarPigType;
                        parameters[6].Value = item.Comment;
                        parameters[17].Value = Common.YuChanHelper.GetYuChanDate(item.HybridizationDate);
                        break;
                    case 2:
                        parameters[7].Value = item.HybridizationDate;
                        parameters[8].Value = item.HybridizationPerson;
                        parameters[9].Value = item.BoarBitID;
                        parameters[10].Value = item.BoarPigType;
                        parameters[11].Value = item.Comment;
                        break;
                    case 3:
                        parameters[12].Value = item.HybridizationDate;
                        parameters[13].Value = item.HybridizationPerson;
                        parameters[14].Value = item.BoarBitID;
                        parameters[15].Value = item.BoarPigType;
                        parameters[16].Value = item.Comment;
                        break;
                }
            }
        }


        #endregion
    }
}


